package org.vector.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.vector.daoImpl.BankDaoImpl;
import org.vector.utils.BaseDao;

@SuppressWarnings("serial")
public class WindowRes extends JFrame{
	BaseDao bd = new BaseDao();
	BankDaoImpl bdi = new BankDaoImpl();
	private JTextField t1,t2,t3,t4,t5,t6,t7,t8;
	private JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
	private JPanel p,down;
	private JButton btn1,btn2;
	private JTextArea tare;
	
	public WindowRes() {
		into();
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	private void into() {
		setSize(250, 600);
		setLocationRelativeTo(null);
		l8 = new JLabel("注册");
		add(l8,BorderLayout.NORTH);
		
		p = new JPanel();
		p.setLayout(new FlowLayout());
		l1 = new JLabel("姓名");
		l2 = new JLabel("密码");
		l3 = new JLabel("再次输入密码");
		l4 = new JLabel("身份证号");
		l5 = new JLabel("银行卡号");
		l6 = new JLabel("银行支付密码");
		l7 = new JLabel("手机号");
		l9 = new JLabel("用户条款");
		
		t1 = new JTextField(20);
		t2 = new JTextField(20);
		t3 = new JTextField(20);
		t4 = new JTextField(20);
		t5 = new JTextField(20);
		t6 = new JTextField(20);
		t7 = new JTextField(20);
		t8= new JTextField(20);
		tare = new JTextArea(6,8);
		//add(new JScrollPane(tare));
		//tare.add(new JScrollPane());
		tare.append("第一条，第二条");
		
		p.add(l1);
		p.add(t1);
		p.add(l2);
		p.add(t2);
		p.add(l3);
		p.add(t3);
		p.add(l4);
		p.add(t4);
		p.add(l5);
		p.add(t5);
		p.add(l6);
		p.add(t6);
		p.add(l7);
		p.add(t7);
		p.add(new JLabel("充值金额"));
		p.add(t8);
		p.add(l9);
		p.add(tare);
		add(p,BorderLayout.CENTER);
		
		
		down = new JPanel();
		btn1 = new JButton("取消");
		btn2 = new JButton("注册");
		down.add(btn1);
		down.add(btn2);
		add(down,BorderLayout.SOUTH);
		
		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				WindowRes.this.dispose();
			}
		});
		
		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = t1.getText();
				//int a = Integer.valueOf(name);
				
				String password = t2.getText();
				int b = Integer.valueOf(password);
				
				String password2 = t3.getText();
				int c = Integer.valueOf(password2);
				
				String idCard = t4.getText();
				int d = Integer.valueOf(idCard);
				
				String ccd = t5.getText();
				int l = Integer.valueOf(ccd);
				
				String ccdpassword = t6.getText();
				int f = Integer.valueOf(ccdpassword);
				
				String phone = t7.getText();
				int g = Integer.valueOf(phone);
				
				String cunkuan = t8.getText();
				int h = Integer.valueOf(cunkuan);
				
				//随机账号
				int zhanghao = (int)(Math.random()*(10000-1000+1)+1000);
				String cc = Integer.toString(zhanghao);
				
				int ww = 0;
				String sql = "select name from bank where name = ?";
				ResultSet resultset = bd.executeQuery(sql,name);
				try {
					while(resultset.next()) {
						new Wcuowu("，姓名已被注册，");
						ww++;
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				String sql2 = "select bankid from bank where bankid = ?";
				ResultSet resultset2 = bd.executeQuery(sql2, zhanghao);
				try {
					while(resultset2.next()) {
						new Wcuowu("，账号已被注册，");
						ww++;
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				if(ww == 0) {
					if(b == c) {
						int rows = bdi.register(zhanghao, name, b, d, l, f, g, h);
						if(rows == 1) {
							WindowRes.this.dispose();
							new Wcuowu(cc);
						}else {
							String ss = ",注册失败";
							new Wcuowu(ss);
						}
					}else {
						String dd = ",两个密码不相同";
						new Wcuowu(dd);
					}
				}
			}
		});
		
	}
//	public static void main(String[] args) {
//		new WindowRes();
//	}
}
